#include "deal_command.h"

#include "log.h"

#include "directory_operation.h"
#include "transfile_server.h"


int dealCommand(userOpInfo_t *info, tlvPacket_t *tlv) {
    // 备份一个info， 给后续函数用
    userOpInfo_t info_dup;
    info_dup.mysql = info->mysql;
    info_dup.netfd = info->netfd;
    info_dup.ID = info->ID;
    strcpy(info_dup.addr, info->addr);
    strcpy(info_dup.userName, info->userName);
    strcpy(info_dup.opName, info->opName);

    // 备份tlv->value
    char value_dup[1024] = {0};
    strcpy(value_dup, info_dup.opName);
    //printf("value_dup: %s\n", value_dup);

    int ret = 1;
    if(tlv->type == CD) {  // cd 201  send int & path
        char command[4096] = {0};
        snprintf(command, sizeof(command), "cd %s", value_dup);
        LOG(info->userName, command);
        printf("%s\n", command);

        char path[4096] = {0};
        ret = cdDir(info, path);
        send(info->netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        if(ret == -1) {
            printf("cd error\n");
            return -1;
        }

        //printf("send to cd path: %s, ret = %d\n", path, ret);

        tlvPacket_t tlvv;
        tlvv.type = CD;
        tlvv.length = strlen(path);
        strncpy(tlvv.value, path, sizeof(tlvv.value));
        send(info->netfd, &tlvv, sizeof(tlvv.type) + sizeof(tlvv.length) + tlvv.length, MSG_NOSIGNAL);


        //printf("cd send ok.\n");
    }
    else if(tlv->type == LS) {  // ls 202  send int & content
        LOG(info->userName, "ls");
        printf("ls\n");

        char content[4096] = {0};
        ret = lsDir(info_dup, content);
        send(info->netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        if(ret == -1) {
            printf("ls error\n");
            return -1;
        }

        printf("send to ls content: %s,  len is %ld\n", content, strlen(content));

        tlvPacket_t tlvv;
        tlvv.type = LS;
        tlvv.length = strlen(content);
        strncpy(tlvv.value, content, sizeof(tlvv.value));
        send(info->netfd, &tlvv, sizeof(tlvv.type) + sizeof(tlvv.length) + tlvv.length, MSG_NOSIGNAL);   
    }
    else if(tlv->type == PWD) {  // pwd 203  send int
        LOG(info->userName, "pwd");
        printf("pwd\n");
    }
    else if(tlv->type == MKDIR) {  // mkdir 204  send int
        char command[4096] = {0};
        snprintf(command, sizeof(command) - 1, "mkdir %s", value_dup);
        LOG(info->userName, command);
        printf("%s\n", command);

        ret = mkDir(info_dup);
        send(info->netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        if(ret == -1) {
            printf("mkdir error\n");
            return -1;
        }
    }
    else if(tlv->type == RMDIR) {  // rmdir 205  send int
        char command[4096] = {0};
        snprintf(command, sizeof(command) - 1, "rmdir %s", value_dup);
        LOG(info->userName, command);
        printf("%s\n", command);

        ret = rmDir(info_dup);
        send(info->netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        if(ret == -1) {
            printf("rmdir error\n");
            return -1;
        }
    }
    else if(tlv->type == RM) {  // remove 206  send int
        char command[4096] = {0};
        snprintf(command, sizeof(command) - 1, "remove %s", value_dup);
        LOG(info->userName, command);
        printf("%s\n", command);

        ret = rmFile(info_dup);
        send(info->netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        if(ret == -1) {
            printf("remove error\n");
            return -1;
        }
    }
    else if(tlv->type == PUTS) {  // puts 301  send int
        char command[4096] = {0};
        snprintf(command, sizeof(command) - 1, "puts %s", value_dup);
        LOG(info->userName, command);
        printf("%s\n", command);

        //printf("ready to send to puts\n");
        ret = puts_server(info);
        send(info->netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        //printf("send done\n");
        if(ret == -1) {
            printf("puts error\n");
            return -1;
        }

        //printf("send to puts ok\n");
    }
    else if(tlv->type == GETS) {  // gets 302  send int
        char command[4096] = {0};
        snprintf(command, sizeof(command) - 1, "gets %s", value_dup);
        LOG(info->userName, command);
        printf("%s\n", command);

        ret = gets_server(info);
        send(info->netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        if(ret == -1) {
            printf("puts error\n");
            return -1;
        }
    }
    else if(tlv->type == ACCOUNTDELETE) {  // logoff 101  send int
        LOG(info->userName, "logoff");
        printf("logoff\n");

        ret = deleteAccount(info);
        send(info->netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        if(ret == -1) {
            printf("logoff error\n");
            return -1;
        }
    }
    else if(tlv->type == TREE) {  // register 501  send int & content
        LOG(info->userName, "tree");
        printf("tree\n");

        char content[4096] = {0};
        ret = treeDir(info_dup, "", content);
        send(info->netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        if(ret == -1) {
            printf("tree error\n");
            return -1;
        }

        printf("send to tree content: %s,  len is %ld\n", content, strlen(content));

        tlvPacket_t tlvv;
        tlvv.type = LS;
        tlvv.length = strlen(content);
        strncpy(tlvv.value, content, sizeof(tlvv.value));
        send(info->netfd, &tlvv, sizeof(tlvv.type) + sizeof(tlvv.length) + tlvv.length, MSG_NOSIGNAL);   
    }
    else {
        printf("命令不合法!\n");
        return -1;
    }

    return 1;
}
